package com.thm.tfast.modules.sys.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thm.tfast.core.entity.ViewResult;
import com.thm.tfast.modules.sys.model.SysMenu;
import com.thm.tfast.modules.sys.model.dto.menu.MenuInsertDto;
import com.thm.tfast.modules.sys.model.dto.menu.MenuUpdateDto;
import com.thm.tfast.modules.sys.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 * 菜单信息 前端控制器
 * </p>
 *
 * @author THM
 * @since 2022-10-10
 */
@RestController
@Api(tags = "系统：菜单管理")
@RequestMapping("/sysMenu")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @ApiOperation("根据条件分页获取菜单列表")
    @GetMapping(value = "/page")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "keyword", value = "关键字：菜单名称"),
            @ApiImplicitParam(name = "parentId", value = "父级ID"),
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true)
    })
    public ViewResult<Page<SysMenu>> page(@RequestParam(value = "keyword", required = false) String keyword,
                                          @RequestParam(value = "parentId", required = false) Long parentId,
                                          @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysMenu> page = sysMenuService.page(keyword, parentId, pageNum, pageSize);
        return ViewResult.success(page);
    }

    @ApiOperation("获取指定菜单信息")
    @GetMapping("/{menuId}")
    public ViewResult<SysMenu> getMenuById(@PathVariable Long menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return ViewResult.success(sysMenu);
    }

    @ApiOperation("新增菜单信息")
    @PostMapping("/insert")
    public ViewResult<Long> insert(@Validated @RequestBody MenuInsertDto dto){
        return ViewResult.success(sysMenuService.insert(dto));
    }

    @ApiOperation("修改菜单信息")
    @PutMapping("/update")
    public ViewResult<String> update(@Validated @RequestBody MenuUpdateDto dto) {
        boolean success = sysMenuService.update(dto);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

    @ApiOperation("删除菜单信息")
    @DeleteMapping("/delete/{menuId}")
    public ViewResult<String> delete(@PathVariable Long menuId){
        boolean success = sysMenuService.delete(menuId);
        if (success) {
            return ViewResult.success();
        }
        return ViewResult.error();
    }

}

